<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>系统</title>
<%@include file="/WEB-INF/jsp/include/easyui_1_5_3.jsp"%>
<script type="text/javascript" src="${path}/js/business/sysUnitInfoComboTree.js"></script>
<script type="text/javascript" src="${path}/js/business/kvComboBox.js"></script>

<script type="text/javascript">

	//请求地址
	var url;
	//提示消息
	var mesTitle;

	//页面加载后执行
	$(function(){
		unitInfoComboTree('#unit_id_form',path+'/sysUnitInfo/getBy');
		kvComboBox('#sex_form',path+'/sysDict/kvByType?pid=xb');
		kvComboBox('#status_form',path+'/sysDict/kvByType?pid=yhzt');
    });
	//添加用户信息
	function addSysUser(){
 		$('#user_id').css('display','block'); 
		$('#dlg').dialog('open').dialog('setTitle','新增');
		$('#fm').form('clear');
		url=basePath+"/sysUser/add";
		mesTitle = '新增成功';
	}
	
	//编辑用户信息
 	function editSysUser(){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (row){
	 		var user_id = row.user_id;
	 		$('#start_date_form').datebox('setValue',row.start_date);
	 		$('#end_date_form').datebox('setValue',row.end_date);
// 	 		$('#user_id').attr('readonly',true);
		 	$('#dlg').dialog('open').dialog('setTitle','编辑');
		 	$('#fm').form('load',row);
		 	//url = path+"/sysUser/modify?user_id="+user_id;
		 	url = path+"/sysUser/modify";
		 	mesTitle = '编辑成功';
	 	}else{
	 		$.messager.alert('提示', '请选择要编辑的记录！', 'error');
	 	}
	}
 	
	//删除用信息
 	function deleteSysUser(){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (row){
	 		var user_id = row.user_id;
		 	$('#dlg_delete').dialog('open').dialog('setTitle','删除');
		 	$('#fm').form('load',row);
		 	url = path+"/sysUser/delete?user_id="+user_id;
		 	mesTitle = '删除成功';
	 	}else{
	 		$.messager.alert('提示', '请选择要删除的记录！', 'error');
	 	}
	}
 	
	//保存添加、修改内容
	function saveSysUser(){
// 		alert(url);
	 	$('#fm').form('submit',{
		 	url: url,
		 	onSubmit: function(){
		 		return $(this).form('validate');
		 	},
			success: function(result){
				/* console.info(result); */
// 				alert(result);
				
				var result = eval('('+result+')');
				if (result.success){
				 	$('#dlg').dialog('close'); 
				 	$('#datagrid').datagrid('reload'); 
				} else {
					 mesTitle = '新增失败';
					 var fileD = result.obj;
					 var errorMsg = "";
					 for(var i=0; i<fileD.length; i++){
						 var veh = fileD[i];//获取LIST里面的对象
// 						 alert(veh.field+":"+veh.errorMsg);
						 errorMsg = errorMsg+veh.field+":"+veh.errorMsg+"<br/>";
					 }
					 $.messager.alert('错误',errorMsg);
				}
				$.messager.show({ 
					 title: mesTitle,
					 msg: result.msg
				});
			}
	 	});
	}	
	
	//提交删除内容
	function saveSysUser_del(){
	 	$('#fmdelete').form('submit',{
		 	url: url,
		 	onSubmit: function(){
		 		return $(this).form('validate');
		 	},
			success: function(result){
				/* console.info(result); */
				var result = eval('('+result+')');
				if (result.success){
				 	$('#dlg_delete').dialog('close'); 
				 	$('#datagrid').datagrid('reload'); 
				} else {
					 mesTitle = '新增失败';
				}
				$.messager.show({ 
					 title: mesTitle,
					 msg: result.msg
				});
			}
	 	});
	}

	//快速查询
 	function searchSysUserQ(){
	 	$("#datagrid").datagrid("load", {
            "username": $('#search_username').val()
        });
	}
	
	//查询
 	function searchSysUser(){
		$('#dlgsearch').dialog('open').dialog('setTitle','查询');
		$('#fmsearch').form('clear');
		url = path+"/user/searchUser";
		mesTitle = '查询成功';
	}
	
	//刷新
	function reload(){
		$('#datagrid').datagrid('reload'); 
	}
//--------------------------------------------
	//编辑权限
	function editRole(id){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (!row){
	 		$.messager.alert('提示', '请选择要编辑的记录！', 'error');
	 	}
	 	var user_id = row.user_id;
	    //1.取消所有选择
	    //var root = $('#juese_tree').tree('getRoot');  
	    //$("#juese_tree").tree('uncheck',root.target);  
	    //2.加载权限，动态选择
	    var url = path+"/sysRoles/getRoleByUser?user_id="+user_id;
	    $.ajax({
	        cache : true,
	        type : "POST",
	        url : url,
	        async : false,
	        success : function(data) {
	            data = JSON.parse(data);
				$(data).each(function(i, obj){
					checkTreeNode(obj);
				});
	        }
	    });
	    
	    $("#juese_win").window('open');
	}
	function checkTreeNode(obj){
        var children = obj.children;
        if(children==''||children==null){
//          $(obj).each(function(j,c_obj){
            var cc = $("#juese_tree").tree('find',obj.id);
            if(cc){
                $("#juese_tree").tree('check',cc.target);
            }
//         });
        }else{
            $(children).each(function(j,c_obj){
        		checkTreeNode(c_obj);
            });
        }
	}
	function checkTreeNode(obj){
        var children = obj.children;
        if(children==''||children==null){
//          $(obj).each(function(j,c_obj){
            var cc = $("#juese_tree").tree('find',obj.id);
            if(cc){
                $("#juese_tree").tree('check',cc.target);
            }
//         });
        }else{
            $(children).each(function(j,c_obj){
        		checkTreeNode(c_obj);
            });
        }
	}
	//提交修改
	function confrimEdit(){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (!row){
	 		$.messager.alert('提示', '请选择要编辑的记录！', 'error');
	 	}
	 	var user_id = row.user_id;
	 	var unit_id = row.unit_id;
	    var roleIds = getChecked();
	    var url = path+"/sysUserRoles/addForUser";
	    var data = {
	            "user_id" : user_id,
	            "unit_id" : unit_id,
	            "roleIds": roleIds
	        };
	    $.ajax({
	        cache : true,
	        type : "POST",
	        url : url,
	        data: data,
	        async : true,
	        success : function(result) {
// 	        	alert(result);
// 				var result = eval('('+result+')');
// 				alert(result.success)
				if (result.success){
	                closeWin();
	                $.messager.show({
	                    msg : result.msg,
	                    title : '成功'
	                });
				} else {
					 mesTitle = '新增失败';
		             $.messager.alert(
		                        '错误',
		                        result.msg,
		                        'error');
				}
	        }
	    });
	}
	//获取选中节点和父节点
	function getChecked(){
	    var nodesParent = $('#juese_tree').tree('getChecked','indeterminate');
	    var nodes = $('#juese_tree').tree('getChecked');
	    var s = '';
	    
	    for (var i = 0; i < nodesParent.length; i++) {
	        if (s != '') 
	            s += ',';
	        s += nodesParent[i].id;
	    }
	    for (var i = 0; i < nodes.length; i++) {
	        if (s != '') 
	            s += ',';
	        s += nodes[i].id;
	    }
	   return s;
	}

	//关闭窗口
	function closeWin(){
	     $('#juese_tree').tree('reload'); 
	     $("#juese_win").window('close');
	}
	
//--------------------------------------------
	
	
	
</script>
</head>
<body class="easyui-layout" fit="true">
	<div region="center" border="false" style="overflow: hidden;">
		<!-- 信息列表 title="管理" -->
		<table id="datagrid" class="easyui-datagrid" 
		    fit="true"
			url="${path}/sysUser/datagrid" 
			toolbar="#toolbar" 
			pagination="true"
			fitColumns="false" 
			singleSelect="false" 
			rownumbers="true"
			striped="true"
			border="false" 
			nowrap="false">
			<thead>
				<tr>
					<th field="unit_id" width="100">单位ID</th>
					<th field="user_id" width="100">账号</th>
					<th field="password" width="100">密码</th>
					<th field="num" width="100">序号</th>
					<th field="user_name" width="100">姓名</th>
					<th field="sex" width="100">性别</th>
					<th field="identity_id" width="100">身份证号</th>
					<th field="birthday" width="100" >出生日期</th>
					<th field="phone" width="100">联系电话</th>
					<th field="email" width="100">电子邮箱</th>
					<th field="role_code" width="100">角色编码</th>
					<th field="department" width="100">部门描述</th>
					<th field="status" width="100">状态(1：启用  2：冻结  3：删除）</th>
					<th field="start_date" width="100" >启用时间</th>
					<th field="end_date" width="100">停用时间</th>
					<th field="handle" width="100">操作人</th>
					<th field="handle_date" width="100">操作时间</th>

				</tr>
			</thead>
		</table>

		<!-- 按钮 -->
		<div id="toolbar">
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-reload" plain="true" onclick="reload();">刷新</a>
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-add" plain="true" onclick="addSysUser();">新增</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editSysUser();">编辑</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-remove" plain="true" onclick="deleteSysUser();">删除</a>
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editRole();">分配角色</a> 
			<span>用户名:</span><input name="search_username" id="search_username" value="" size=10 /> 
  			<a href="javascript:searchSysUserQ()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-jright" plain="true" onclick="searchSysUser();">更多查询</a>
		</div>

		<!-- 添加/修改对话框 -->
		<div id="dlg" class="easyui-dialog"
			style="width:600px;height:400px;padding:30px 20px" closed="true"
			buttons="#dlg-buttons">
			<form id="fm" method="post" novalidate>
				<table>
					<tr>
						<td>
							<label for="unit_id">单位ID:</label>
						</td>
						<td>
<!-- 							<input name="unit_id" class="easyui-validatebox" required="true"> -->
							<select id="unit_id_form" name="unit_id" class="easyui-combotree" required="true" />
						</td>
						<td>
							<label>账号:</label>
						</td>
						<td>
							<input name="user_id" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>密码:</label>
						</td>
						<td>
							<input name="password" type="password" class="easyui-validatebox" required="true">
						</td>
						<td>
							<label>序号:</label>
						</td>
						<td>
							<input name="num" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>姓名:</label>
						</td>
						<td>
							<input name="user_name" class="easyui-validatebox" required="true">
						</td>
						<td>
							<label>性别:</label>
						</td>
						<td>
<!-- 							<input name="sex" class="easyui-validatebox" required="true"> -->
							<input id="sex_form" name="sex" class="easyui-combobox" required="false">
						</td>
					</tr>
					<tr>
						<td>
							<label>身份证号:</label>
						</td>
						<td>
							<input name="identity_id" class="easyui-validatebox" required="true">
						</td>
						<td>
							<label>出生日期:</label>
						</td>
						<td>
							<input name="birthday"  type="text" class="easyui-datebox" required="required">
						</td>
					</tr>
					<tr>
						<td>
							<label>联系电话:</label>
						</td>
						<td>
							<input name="phone" class="easyui-validatebox" required="true">
						</td>
						<td>
							<label>电子邮箱:</label>
						</td>
						<td>
							<input name="email" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>角色编码:</label>
						</td>
						<td>
							<input name="role_code" class="easyui-validatebox" >
						</td>
						<td>
							<label>部门描述:</label>
						</td>
						<td>
							<input name="department" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
<!-- 						(1：启用  2：冻结  3：删除） -->
							<label>状态:</label>
						</td>
						<td>
<!-- 							<input name="status" class="easyui-validatebox" required="true"> -->
							<input id="status_form" name="status" class="easyui-combobox" required="true">
						</td>
						<td>
							<label>启用时间:</label>
						</td>
						<td>
							<input id="start_date_form" name="start_date" class="easyui-datebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>停用时间:</label>
						</td>
						<td>
							<input id="end_date_form" name="end_date" class="easyui-datebox" required="true">
						</td>
						<td>
						</td>
						<td>
						</td>
					</tr>
<!-- 					<tr> -->
<!-- 						<td> -->
<!-- 							<label>操作人:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="handle" class="easyui-validatebox" required="true"> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<label>操作时间:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="handle_date" class="easyui-datebox" required="true"> -->
<!-- 						</td> -->
<!-- 					</tr> -->
				</table>
			</form>
		</div>
		
		<!-- 添加/修改对话框按钮 -->
		<div id="dlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6"
				iconCls="icon-ok" onclick="saveSysUser()" style="width:90px">保存</a> 
				<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')"
				style="width:90px">取消</a>
		</div>
		
		<!-- 删除对话框 -->
		<div id="dlg_delete" class="easyui-dialog"
			style="width:300px;height:200px;padding:30px 20px" closed="true"
			buttons="#dlg-del-buttons">
			<div class="ftitle">请谨慎操作</div>
			<form id="fmdelete" method="post" novalidate>
					<label>确定删除吗？</label>
			</form>
		</div>
		
		<!-- 删除对话框按钮 -->
		<div id="dlg-del-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6"
				iconCls="icon-ok" onclick="saveSysUser_del()" style="width:90px">删除</a> 
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg_delete').dialog('close')"
				style="width:90px">取消</a>
		</div>
		
		<!-- 查询对话框 -->
		<div id="dlgsearch" class="easyui-dialog"
			style="width:400px;height:380px;padding:30px 20px" closed="true"
			buttons="#dlg-buttons">
			<form id="fmsearch" method="post" novalidate>
				<label>权限:</label>  
                    <span id="span1" style="display: inline-block;"></span>  
                </div>
			</form>
		</div>
		
		<!-- 查询对话框按钮 -->
		<div id="dlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6"
				iconCls="icon-ok" onclick="searchSysUser()" style="width:90px">查询</a> 
				<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlgsearch').dialog('close')"
				style="width:90px">取消</a>
		</div>
		<!-- 角色编辑窗口 -->
		<div id="juese_win" class="easyui-window" title="角色编辑"
		    style="width: 500px; height: 250px;" closed="true">
		    <form style="padding: 10px 20px 10px 40px;">
		        <ul id="juese_tree" class="easyui-tree" url="<%=path %>/sysRoles/getRoleBy" method="post"
		            checkbox="true">
		        </ul>
		        <div style="padding: 5px; text-align: center;">
		            <a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="confrimEdit();">确定</a>
		            <a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="closeWin();">取消</a>
		        </div>
		    </form>
		</div>
		
	</div>
</body>
</html>
